package com.example.md5;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class MD5
{
	private char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7','8', '9','A', 'B', 'C', 'D', 'E', 'F' };  
	// MD5字符串加密，32位
	public static String MD5(String str) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}

		char[] charArray = str.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++) {
			byteArray[i] = (byte) charArray[i];
		}
		byte[] md5Bytes = md5.digest(byteArray);

		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();
	}

	// 可逆的加密算法
	public static String encryptmd5(String str) {
		char[] a = str.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 'l');
		}
		String s = new String(a);
		return s;
	}
	
	public String toHexString(byte[] b)   
	{  
		StringBuilder sb = new StringBuilder(b.length * 2);  
		for (int i = 0; i < b.length; i++)   
		{  
			sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);  
			sb.append(HEX_DIGITS[b[i] & 0x0f]);  
		}  
		return sb.toString();  
	}  
	
	public String md5sum(String filename)   
	{  
		InputStream fis;  
		byte[] buffer = new byte[1024];  
		int numRead = 0;  
		MessageDigest md5;  
		try   
		{  
			fis = new FileInputStream(filename);  
			md5 = MessageDigest.getInstance("MD5");  
			while ((numRead = fis.read(buffer)) > 0)   
			{  
				md5.update(buffer, 0, numRead);  
			}  
			fis.close();  
			return toHexString(md5.digest());   }   
		catch (Exception e)  
		{  
			System.out.println("error");  
			return null;  
		}  
	}  
	
	
}
